package canglang.monitor.cases.dao;

import canglang.monitor.cases.model.CaseSixModel;
import canglang.monitor.cases.model.CaseSevenModel;
import canglang.monitor.cases.to.SearchSixTO;
import canglang.monitor.cases.to.SearchSevenTO;
import canglang.monitor.core.base.dao.BaseDAO;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.StringUtil;

import java.util.*;

@SuppressWarnings("unchecked")
public class CaseSevenDAO extends BaseDAO {
    public Integer addCaseSeven(CaseSevenModel caseSevenModel) {
        String sql = "addCaseSeven";
        Map map = objectToMap(caseSevenModel);
        String cnt = StringUtil.convertEmpty2Null(caseSevenModel.getContent());
        map.put("content", cnt);
        return (Integer) getSqlMapClientTemplate().insert(sql, map);
    }

    public CaseSevenModel getCaseSeven(int caseSevenId) {
        String sql = "getCaseSeven";
        Map map = new HashMap();
        map.put("caseSevenId", caseSevenId);
        return (CaseSevenModel) getSqlMapClientTemplate().queryForObject(sql, map);
    }

    public CaseSevenModel saveCaseSeven(CaseSevenModel caseSevenModel) {
        String sql = "saveCaseSeven";
        Map map = objectToMap(caseSevenModel);
        String cnt = StringUtil.convertEmpty2Null(caseSevenModel.getContent());
        map.put("content", cnt);
        getSqlMapClientTemplate().update(sql, map);
        return this.getCaseSeven(caseSevenModel.getCaseSevenId());
    }

    public void deleteCaseSeven(int caseSevenId) {
        String sql = "deleteCaseSeven";
        Map map = new HashMap();
        map.put("caseSevenId", caseSevenId);
        getSqlMapClientTemplate().delete(sql, map);
    }

    public Integer searchCount(SearchSevenTO searchTO) {
        Map map = new HashMap();
        map.put("dateFrom", searchTO.getDateFrom());
        if (searchTO.getDateTo() != null) {
            map.put("dateTo", DateUtil.add(searchTO.getDateTo(), Calendar.DATE, 1));
        }
        map.put("createId", StringUtil.convertEmpty2Null(searchTO.getCreateId()));
        return (Integer) getSqlMapClientTemplate().queryForObject("searchCaseSevenCount", map);
    }

    public List searchList(PaginatedListHelper helper, SearchSevenTO searchTO) {
        Map map = new HashMap();
        map.put("dateFrom", searchTO.getDateFrom());
        if (searchTO.getDateTo() != null) {
            map.put("dateTo", DateUtil.add(searchTO.getDateTo(), Calendar.DATE, 1));
        }
        map.put("createId", StringUtil.convertEmpty2Null(searchTO.getCreateId()));
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchCaseSevenList", map);
    }

    public List getCaseSevenList(SearchSevenTO searchTO) {
        Map map = new HashMap();
        Date dateFrom = searchTO.getDateFrom();
        Date dateTo = searchTO.getDateTo();
        map.put("dateFrom", dateFrom);
        if (dateTo != null) {
            map.put("dateTo", DateUtil.add(dateTo, Calendar.DATE, 1));
        }
        return getSqlMapClientTemplate().queryForList("getCaseSevenList", map);
    }
}